//
//  TabBarViewController.m
//  FreshBee
//
//  Created by Goofy on 15/10/2016.
//  Copyright © 2016 iOS013. All rights reserved.
//

#import "TabBarViewController.h"
#import "HomeTableViewController.h"
#import "MarketViewController.h"
#import "CartTableViewController.h"
#import "MineTableViewController.h"
#import "TabBar.h"
#import "UIImage+Color.h"
#import "UIButton+NewButton.h"
#import "CKBaseNAVController.h"
#import "ScanCodeViewController.h"
@interface TabBarViewController ()

@property (nonatomic,strong) HomeTableViewController *htvc;

@property (nonatomic,strong) MarketViewController *mvc;

@property (nonatomic,strong) CKBaseNAVController *bnav;

@end

@implementation TabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    TabBar *tabBar = [[TabBar alloc] init];
    [self setValue:tabBar forKey:@"tabBar"];
    
    HomeTableViewController *home = [[HomeTableViewController alloc] init];
    self.htvc = home;
    [self addChildViewController:home withImage:[UIImage imageNamed:@"v2_home"] selectedImage:[UIImage imageNamed:@"v2_home_r"] withTittle:@"首页"];
    [self setNavBarwithController:home imageName:@"icon_black_scancode" title:@"扫一扫"];
    [self setRightNavBarwithController:home imageName:@"icon_search" title:@"搜索"];
    
    MarketViewController *market = [[MarketViewController alloc] init];
    self.mvc = market;
    [self addChildViewController:market withImage:[UIImage imageNamed:@"v2_order"] selectedImage:[UIImage imageNamed:@"v2_order_r"] withTittle:@"闪送超市"];
    [self setNavBarwithController:market imageName:@"icon_black_scancode" title:@"扫一扫"];
    [self setRightNavBarwithController:market imageName:@"icon_search" title:@"搜索"];
    
    
    CartTableViewController *cart = [[CartTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    [self addChildViewController:cart withImage:[UIImage imageNamed:@"shopCart"] selectedImage:[UIImage imageNamed:@"shopCart_r"] withTittle:@"购物车"];
    
    MineTableViewController *mine = [[MineTableViewController alloc] init];
    [self addChildViewController:mine withImage:[UIImage imageNamed:@"v2_my"] selectedImage:[UIImage imageNamed:@"v2_my_r"] withTittle:@"我的"];
}


- (void) setRightNavBarwithController:(UIViewController *)controller imageName:(NSString *)name title:(NSString *)str{
    UIButton *btn = [[UIButton alloc] init];
    [btn setBounds:CGRectMake(0, 0, 40, 40)];
    [btn setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
    [btn setTitle:str forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:10]];
    [btn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:5];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    controller.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    [btn setContentEdgeInsets:UIEdgeInsetsMake(0, 10, 0, -10)];
    [btn addTarget:self action:@selector(clickToSearch) forControlEvents:UIControlEventTouchUpInside];
}

- (void)clickToSearch{
    
}


- (void) setNavBarwithController:(UIViewController *)controller imageName:(NSString *)name title:(NSString *)str{
    [controller.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:252.0/255 green:211.0/255 blue:69.0/255 alpha:1]] forBarMetrics:(UIBarMetricsDefault)];
    UIButton *btn = [[UIButton alloc] init];
    [btn setBounds:CGRectMake(0, 0, 40, 40)];
    [btn setImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
    [btn setTitle:str forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:10]];
    [btn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:5];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    controller.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    [btn setContentEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 10)];
    [btn addTarget:self action:@selector(clickToCode) forControlEvents:UIControlEventTouchUpInside];
}

//跳转二维码
- (void)clickToCode{
    ScanCodeViewController *dvc = [[ScanCodeViewController alloc] init];
    
    if (self.selectedIndex == 0) {
        [self.childViewControllers[0] pushViewController:dvc animated:YES];
    }
    
    if (self.selectedIndex == 1) {
        [self.childViewControllers[1] pushViewController:dvc animated:YES];
    }
}

- (void)addChildViewController:(UIViewController *)controller withImage:(UIImage *)image selectedImage:(UIImage *)selectImage withTittle:(NSString *)tittle{
    
    CKBaseNAVController *nav = [[CKBaseNAVController alloc] initWithRootViewController:controller];
    
    self.bnav = nav;
    
    [nav.tabBarItem setImage:image];
    
    [nav.tabBarItem setSelectedImage:selectImage];
    
    controller.title = tittle;
    
    [nav.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor]} forState:UIControlStateSelected];
    
    nav.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -3);
    
    [self addChildViewController:nav];
    
}
@end




